package com.gang.common.lib.utils;

import com.gang.common.lib.annotation.AnnoField;
import com.gang.common.lib.exception.CommonException;

import java.lang.reflect.Field;

/**
 * @Classname AnnotationUtils
 * @Description TODO
 * @Date 2020/6/27 22:37
 * @Created by zengzg
 */
public class AntAnnotationUtils {

    public static void checkFieldAnno(Object object) {
        Field[] fieldArray = object.getClass().getDeclaredFields();
        for (Field field : fieldArray) {
            AnnoField annoField = getAnnoField(field);
            if (annoField != null && !annoField.nullable()) {
                try {
                    if (null == field.get(object)) {
                        throw new CommonException(object.getClass().getSimpleName() + " Field " + annoField.name() + "can't null");
                    }
                } catch (IllegalAccessException e) {
                }
            }
        }
    }

    /*
     * TO对象字段注解ldap属性获取
     */
    public static AnnoField getAnnoField(Field field) {
        AnnoField[] annoFields = field.getAnnotationsByType(AnnoField.class);
        return annoFields == null || annoFields.length > 0 ? annoFields[0] : null;
    }
}
